package C049;

/**
 * ClassName: C03
 * Package: C049
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/2/17 10:21
 * @Version 1.0
 * @Since 1.0
 */
public class C03 {
    public static void main(String[] args) {
//        String s = "ADOBECODEBANC";
        String s = "a";
//        String t = "ABC";
        String t = "aa";
        System.out.println(minWindow(s, t));
    }

    // 字符串
    public static String minWindow(String s, String t) {
        char[] sarr = s.toCharArray();
        char[] tarr = t.toCharArray();

        // 更新数据
        int[] cnt = new int[256];
        for (int i = 0; i < tarr.length; i++) {
            cnt[tarr[i]] --;
        }
        int debt = -tarr.length;

        // 获取数组的数据
        int len = Integer.MAX_VALUE, start = Integer.MAX_VALUE;
        for (int l = 0, r = 0; r < sarr.length; r++) {
            if(cnt[sarr[r]] ++ < 0) {
                debt ++;
            }
            if(debt == 0) {
                // 字符串是否能够压缩
                while(cnt[sarr[l]] > 0) {
                    cnt[sarr[l++]] --;
                }
                if(r - l + 1 < len) {
                    len = r - l + 1;
                    start = l;
                }
            }
        }

        return len == Integer.MAX_VALUE ? "" : s.substring(start, start + len);
    }
}
